package com.mobile.manager.controller;

import com.mobile.manager.pojo.Device;
import com.mobile.manager.service.IDeviceService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Created by zhangdi on 16/5/30.
 */
@Controller
@RequestMapping("/device")
public class DeviceManagerController {
    @Resource
    private IDeviceService deviceService;
    //新增设备
    @RequestMapping(value = "/addDevice", method = RequestMethod.POST)
    public String addDevice(HttpServletRequest request,Model model) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String name = request.getParameter("deviceName");
        String device_number= request.getParameter("device_number");
        String os = request.getParameter("optionsRadios");
        String os_version = request.getParameter("os_version");
        String resolution = request.getParameter("resolution");
        String cpu = request.getParameter("cpu");
        String frequency = request.getParameter("frequency");
        String memory_ysize = request.getParameter("memory_ysize");
        String group = request.getParameter("group");
        String owner = request.getParameter("owner");
        String date = "2015-01-01 12:00:00";
        date = request.getParameter("startTime");
        System.out.println("++++++++++++++"+date);
        Device device = new Device();
        device.setDevice_name(name);
        device.setDevice_number(device_number);
        device.setOs(os);
        device.setOs_version(os_version);
        device.setResolution(resolution);
        device.setCpu(Integer.valueOf(cpu));
        device.setFrequency(Integer.valueOf(frequency));
        device.setMemory_ysize(Integer.valueOf(memory_ysize));
        device.setGroup_info(group);
        device.setOwner(owner);
        device.setBuy_time(sdf.parse(date));
        int result = this.deviceService.insertDevice(device);
        return  "addDeviceSuccess";
    }

    //更新设备
    @RequestMapping(value = "/updateDevice", method = RequestMethod.POST)
    public String updateDevice(HttpServletRequest request,Model model) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String id= request.getParameter("deviceId");

        String name = request.getParameter("deviceName");
        String device_number= request.getParameter("device_number");
        String os = request.getParameter("optionsRadios");
        String os_version = request.getParameter("os_version");
        String resolution = request.getParameter("resolution");
        String cpu = request.getParameter("cpu");
        String frequency = request.getParameter("frequency");
        String memory_ysize = request.getParameter("memory_ysize");
        String group = request.getParameter("group");
        String owner = request.getParameter("owner");
        String date = "2015-01-01 12:00:00";
        date = request.getParameter("startTime");
        System.out.println("++++++++++++++"+date);
        Device device = new Device();

        Integer ID = Integer.parseInt(id);
        device.setId(ID);
        device.setDevice_name(name);
        device.setDevice_number(device_number);
        device.setOs(os);
        device.setOs_version(os_version);
        device.setResolution(resolution);
        device.setCpu(Integer.valueOf(cpu));
        device.setFrequency(Integer.valueOf(frequency));
        device.setMemory_ysize(Integer.valueOf(memory_ysize));
        device.setGroup_info(group);
        device.setOwner(owner);
        device.setBuy_time(sdf.parse(date));
        int result = this.deviceService.updateDevice(device);
        return  "editDeviceSuccess";
    }

    //借用手机
    @RequestMapping(value = "/borrowDevice", method = RequestMethod.POST)
    public  String borrowDevice(HttpServletRequest request,Model model) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String id = request.getParameter("deviceID");
        String borrower = request.getParameter("borrower");
        String deviceName = request.getParameter("deviceName");
        String date = "2015-01-01 12:00:00";
        date = request.getParameter("startTime");
        System.out.println("++++++++++++++"+date);
        Device device = new Device();
        device.setId(Integer.valueOf(id));
        device.setBorrower(borrower);
        device.setGiveback_time(sdf.parse(date));
        int result = this.deviceService.borrowDevice(device);
        return  "borrowSuccess";
    }

}
